package com.ambow.store.controller;

import com.ambow.store.controller.util.Result;
import com.ambow.store.entity.Cart;
import com.ambow.store.service.CartService;
import com.ambow.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController extends BaseController{

    @Autowired
    private CartService cartService;

    @PostMapping("/addtocart")
    public Result addToCart(Cart cart, HttpSession session){
        cartService.addToCart(cart,session);
        return new  Result(OK);
    }

    @GetMapping
    public Result getVOByUid(HttpSession session){
        List<CartVO> voByUid = cartService.findVOByUid(getUidFromSession(session));
        return new Result(OK,voByUid);
    }

    @PutMapping("/num/add/{cid}")
    public Result addNum(@PathVariable("cid") Integer cid,HttpSession session){
        Integer data = cartService.addNum(cid, getUidFromSession(session), getUserNameFromSession(session));
        return new Result(OK,data);
    }

    @GetMapping("/list")
    public Result getVOByCid(Integer[] cids , HttpSession session){
        List<CartVO> voByCid = cartService.getVOByCid(getUidFromSession(session), cids);
        return new Result(OK,voByCid);
    }
}
